﻿using Entity.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using Tools;

namespace Services
{
    public class AgentPayment35: IAgentPayment
    {
        #region 配置及验证

        private string appID = ConfigurationManager.AppSettings["appID"].ToString();

        private string appSecret = ConfigurationManager.AppSettings["appSecret"].ToString();

        private string url = ConfigurationManager.AppSettings["url"].ToString();

        /// <summary>
        /// 获取签名
        /// </summary>
        /// <param name="bodyJson"></param>
        /// <param name="nonce"></param>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        private string GetSignature(string bodyJson, string nonce, string timestamp)
        {
            string bodyMd5 = MD5Helper.UserMd5(Encoding.UTF8.GetBytes(bodyJson)).ToLower();
            return MD5Helper.UserMd5(appID + timestamp + nonce + bodyMd5 + appSecret).ToUpper();
        }

        /// <summary>
        /// 获取请求头
        /// </summary>
        /// <param name="bodyJson"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetHeaders(string bodyJson)
        {
            string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            var nonce = new Random().Next().ToString();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("appid", appID);
            dic.Add("timestamp", timestamp);
            dic.Add("nonce", nonce);
            dic.Add("signature", GetSignature(bodyJson, nonce, timestamp));
            return dic;
        }
        #endregion

        #region 业务

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public override ResultModel QueryUserInfo(dynamic agent)
        {
            const string route = "/v1/transaction/getMeterInfo";
            string reqJson = "{\"meterRealNum\":\"" + agent.userId.ToString() + "\"}";
            return SerPost(reqJson, route);
        }

        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="agent"></param>
        /// <returns></returns>
        public override ResultModel Pay(dynamic agent)
        {
            const string orderRoute = "/v1/transaction/payOrder";
            const string payRoute = "/v1/transaction/payRecharge";
            string orderReqjson = "{\"meterRealNum\":\"" + agent.userId.ToString() + "\",\"payMonery\":" + Convert.ToDecimal(agent.payMoney) + "}";
            var orderResult =  SerPost(orderReqjson, orderRoute);
            if (orderResult.Code == ResultType.Success)
            {
                var resData = orderResult.Data;
                string payReqjson = "{\"bankWaterNum\":\"" + agent.bankWaterNum.ToString() + "\",\"orderWaterNum\":\"" + resData["orderWaterNum"].ToString() + "\",\"meterRealNum\":\"" + agent.userId.ToString() + "\",\"payMonery\":" + Convert.ToDecimal(agent.payMoney) + "}";
                return SerPost(payReqjson, payRoute);
            }
            return orderResult;
        }

        /// <summary>
        /// 交易冲正
        /// </summary>
        /// <param name="bankWaterNum"></param>
        /// <param name="orderWaterNum"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public override ResultModel Reversal(dynamic agent)
        {
            const string route = "/v1/transaction/reversePayment";
            string reqJson = "{\"bankWaterNum\":\"" + agent.bankWaterNum.ToString() + "\",\"orderWaterNum\":\"" + agent.orderWaterNum.ToString() + "\",\"meterRealNum\":\"" + agent.userId.ToString() + "\"}";
            return SerPost(reqJson, route);
        }

        /// <summary>
        /// 交易状态查询
        /// </summary>
        /// <param name="agent"></param>
        /// <returns></returns>
        public override ResultModel QueryOrder(dynamic agent)
        {
            const string route = "/v1/transaction/query";
            string reqJson = "{\"bankWaterNum\":\"" + agent.bankWaterNum.ToString() + "\",\"orderWaterNum\":\"\",\"meterRealNum\":\"" + agent.userId.ToString() + "\"}";
            return SerPost(reqJson, route);
        }

        private ResultModel SerPost(string reqJson,string route)
        {
            string errMsg = string.Empty;
            string resJson = HttpHelper.Post(GetHeaders(reqJson), reqJson, url + route, out errMsg);

            if (!string.IsNullOrWhiteSpace(resJson))
            {
                return JsonConvert.DeserializeObject<ResultModel>(resJson);
            }
            return new ResultModel(ResultType.Error, errMsg);
        }

        #endregion
    }
}
